//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace Microsoft.Research.DataLayer
{
    partial class ActivitySequence
    {
        public Activity Child
        {
            get { return this.Activity; }
        }

        public static int ComparePosition(ActivitySequence seq1, ActivitySequence seq2)
        {
            return seq1.Position.CompareTo(seq2.Position);
        }

        public List<ActivitySequence> Children
        {
            get
            {
                ActivitySequenceCollection<ActivitySequence> seq = this.ActivitySequences;
                List<ActivitySequence> seqList = new List<ActivitySequence>();
                seqList.AddRange(seq);
                seqList.Sort(ComparePosition);
                return seqList;
            }
        }

        /// <summary>
        /// Fetches the base activity sequence.
        /// </summary>
        /// <param name="actSeq">The act seq.</param>
        /// <returns></returns>
        public static ActivitySequence FetchBaseActivitySequence(ActivitySequence actSeq)
        {
            ActivitySequence parentSeq = null;
            if (actSeq != null)
            {
                if (null == actSeq.Parent)
                {
                    parentSeq = actSeq;
                }
                else
                {
                    parentSeq = FetchBaseActivitySequence(actSeq.Parent);
                }
            }

            return parentSeq;
        }

        /// <summary>
        /// Deletes the activity sequence.
        /// </summary>
        public void DeleteActivitySequence()
        {
            foreach (ParameterAssignment paramAssignment in ParameterAssignments)
            {
                paramAssignment.Delete();
            }

            foreach (ActivitySequence activityseq in ActivitySequences)
            {
                activityseq.DeleteActivitySequence();
            }

            Delete();
        }

    }

    /// <summary>
    /// The comparer for an activity sequence.
    /// </summary>
    public class ActivitySequenceComparer : IEqualityComparer<ActivitySequence>
    {
        #region IEqualityComparer<ActivitySequence> Members

        /// <summary>
        /// Determines whether the specified objects are equal.
        /// </summary>
        /// <param name="x">The first object of type <paramref name="T"/> to compare.</param>
        /// <param name="y">The second object of type <paramref name="T"/> to compare.</param>
        /// <returns>
        /// true if the specified objects are equal; otherwise, false.
        /// </returns>
        public bool Equals(Microsoft.Research.DataLayer.ActivitySequence x, Microsoft.Research.DataLayer.ActivitySequence y)
        {
            return (x.Activity as IObject).ID.Equals((y.Activity as IObject).ID);
        }

        /// <summary>
        /// Returns a hash code for the specified object.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> for which a hash code is to be returned.</param>
        /// <returns>A hash code for the specified object.</returns>
        /// <exception cref="T:System.ArgumentNullException">The type of <paramref name="obj"/> is a reference type and <paramref name="obj"/> is null.</exception>
        public int GetHashCode(Microsoft.Research.DataLayer.ActivitySequence obj)
        {
            return (obj as IObject).ID.GetHashCode();
        }

        #endregion
    }

}
